package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

@Controller
public class TestController {

    @RequestMapping("/baseCluster")
    public String baseCluster(Model model, HttpServletRequest request){
        model.addAttribute("runpath", getRunPath(request));
        return "base-cluster";
    }

    @RequestMapping("/baseSessionCluster")
    public String baseSessionCluster(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        String token = (String)session.getAttribute("token");
        if(token == null){
            token = String.valueOf(new Random().nextInt(10000));
            session.setAttribute("token", token);
        }

        model.addAttribute("runpath", getRunPath(request));
        model.addAttribute("token", token);
        return "base-session-cluster";
    }

    public String getRunPath(HttpServletRequest request){
        return request.getServletContext().getRealPath("/");
    }
}
